package org.chuanyang.mm;

import java.util.List;

import org.chuanyang.mm.OperateBillAcitity.CategoryHandler;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;

public abstract class CategoryBaseListActivity extends ListActivity {

	protected static int[] CHOICE_LAYOUT = {
			android.R.layout.simple_list_item_single_choice,
			android.R.layout.simple_list_item_multiple_choice };
	protected static int[] CHOICE_MODE = { ListView.CHOICE_MODE_SINGLE,
			ListView.CHOICE_MODE_MULTIPLE };

	protected MMApp mmApp;
	protected CategoryHandler categoryHandler;
	protected String category;
	protected int get_category_flag = -1;
	protected Button submit;
	protected List<String> data;
	protected ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_category);

		submit = (Button) findViewById(R.id.btn_edit_category_submit);
		// handler = new Handler(getMainLooper());
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
			get_category_flag = bundle.getInt("get_category_flag", -1);
			category = bundle.getString("category");
		}

		mmApp = (MMApp) getApplication();
		categoryHandler = mmApp.getHandler();

		configSubmitBtn(submit);
		data = getData();
		listView = getListView();
		configListView(listView);
		initListView(listView);
	}

	private void initListView(ListView listView) {
		if (category != null) {
			int p = data.indexOf(category);
			listView.setSelection(p);
			listView.setItemChecked(p, true);
		}
	}

	private void configListView(ListView listView) {

		listView.setItemsCanFocus(false);
		listView.setChoiceMode(CHOICE_MODE[0]);
		CategoryAdapter categoryAdapter = new CategoryAdapter(this, data,
				CHOICE_LAYOUT[0]);
		listView.setAdapter(categoryAdapter);

	}

	abstract void configSubmitBtn(Button submit);

	abstract List<String> getData();
}
